import Stroe from '../store'
// 全局字典配置
let dictRenderData = {
  DICT: {},
  NEQ: {},
  TREEDICT: {},
  REGION: {},
}

export function getDictData(t, k) {
  return dictRenderData[t][k] || null
}

export function setDictData(k, v) {
  Object.assign(dictRenderData[k], v)
}

/**
 * @param {String} t 字典类型
 * @param {String} k 字典编码
 * @returns {Array} 返回获取到的字典数组
 */
export function getAjaxDICT(t, k) {
  const baseUrl =
    window.location.port === '9600'
      ? `${window.location.origin}/prod-api`
      : window.location.origin
  let url = ''
  switch (t) {
    case 'DICT':
      url = `${baseUrl}/msbw-base-system/drmsDict/dataDictQuery`
      break
    case 'NEQ':
      url = `${baseUrl}/msbw-base-system/neq/dataNeqQuery`
      break
    case 'TREEDICT':
      url = `${baseUrl}/msbw-base-system/dicts/dataDictsQuery`
      break
    case 'REGION':
      url = `${baseUrl}/msbw-base-system/region/dataRegionQuery`
      break
    default:
      url = `${baseUrl}/msbw-base-system/drmsDict/dataDictQuery`
      break
  }
  return new Promise(async (resolve, reject) => {
    await $.ajax({
      url: url,
      type: 'get',
      async: false,
      data: { dictTypes: k },
      headers: Stroe.requestHeader,
      success: function(res) {
        if (res.code === 1) {
          let OB = {}
          if (t === 'DICT') {
            res.data.forEach((item) => {
              OB[item.DICT_CODE] = item.DICT_ITEMS
            })
          } else if (t === 'NEQ') {
            res.data.forEach((item) => {
              OB[item.NEQ_CODE] = item.NEQ_ITEMS
            })
          } else if (t === 'TREEDICT') {
            res.data.forEach((item) => {
              OB[item.DICTS_CODE] = item.DICT_ITEMS
            })
          } else if (t === 'REGION') {
            res.data.forEach((item) => {
              OB[item.REGION_CODE] = item.REGION_ITEMS
            })
          }
          resolve(OB)
        } else {
          resolve([])
        }
      },
      error: function(res) {
        reject(res)
      },
    })
  })
}
